home *** CD-ROM | disk | FTP | other *** search
- /* ---------------------------------------------------------------------------------------------
- Find_icon, code for constructing icon suites for files and folders
-
- by James W. Walker
- preferred e-mail: <mailto:jwwalker@kagi.com>
- alternate e-mail: <mailto:jwwalker@aol.com>, <jim@nisus-soft.com>
- web: <http://users.aol.com/jwwalker/>
-
- File: Get_normal_file_icon.c
-
- Copyright ©1997 by James W. Walker
-
- You may incorporate this sample code into your applications without
- restriction, though the sample code has been provided "AS IS" and the
- responsibility for its operation is 100% yours.
- If you're going to re-distribute the source, please make it clear
- that the code was descended from James W. Walker's code,
- but that you've made changes.
- ---------------------------------------------------------------------------------------------
- */
-
- #include <Finder.h>
- #include <Folders.h>
- #include <Icons.h>
- #include <LowMem.h>
- #include "Get_normal_file_icon.h"
- #include "cheap-exceptions.h"
- #include "Find_generic_icon_id.h"
- #include "Copy_each_icon.h"
- #include "Get_resource_icons.h"
- #include "DTGetIconSuite.h"
-
- static Boolean Is_local_volume( short vRefNum )
- {
- GetVolParmsInfoBuffer vol_parms;
- OSErr err;
- HIOParam opb;
-
- opb.ioBuffer = (Ptr)&vol_parms;
- opb.ioReqCount = sizeof(vol_parms);
- opb.ioVRefNum = vRefNum;
- opb.ioNamePtr = NULL;
- err = PBHGetVolParmsSync( (HParmBlkPtr) &opb );
-
- return (err != noErr) || (vol_parms.vMServerAdr == 0);
- }
-
- /* ------------------------------------------------------------------
- Get_normal_file_icon Get an icon suite for a file that does
- not have a custom icon.
- ------------------------------------------------------------------
- */
-
-
- OSErr Get_normal_file_icon(
- /* --> */ const CInfoPBRec *cpb,
- /* --> */ MetaSelectorValue icon_selector,
- /* <-- */ Handle *the_suite
- )
- {
- short icon_id;
- EGenericIconLocation inWhere;
- short save_resfile, Finder_resfile, sys_vRefNum;
- long sys_dirID;
- OSErr err;
- short orig_vRefNum;
- IconSelectorValue second_selector;
- VolumeParam vpb;
- OSType fileType = cpb->hFileInfo.ioFlFndrInfo.fdType;
-
- ExpandMetaSelector( icon_selector, &icon_selector, &second_selector );
-
- icon_id = Find_generic_icon_id( fileType, &inWhere );
- save_resfile = CurResFile();
-
- if (inWhere == kGenericIconInFinder)
- {
- (void) FindFolder( kOnSystemDisk, kSystemFolderType,
- kDontCreateFolder, &sys_vRefNum, &sys_dirID );
- SetResLoad( false );
- Finder_resfile = HOpenResFile( sys_vRefNum, sys_dirID,
- LMGetFinderName(), fsRdPerm );
- SetResLoad( true );
- forbid_action( Finder_resfile == -1, HOpenResFile,
- err = ResError() );
- err = Get1_resource_icons( the_suite, icon_id, icon_selector );
- if ( (*the_suite == NULL) && (second_selector != 0) )
- {
- err = Get1_resource_icons( the_suite, icon_id, second_selector );
- }
- CloseResFile( Finder_resfile );
- HOpenResFile: ;
- }
- else // icons in desktop DB or in System
- {
- if (fileType == kApplicationAliasType)
- {
- fileType = 'APPL';
- }
-
- orig_vRefNum = cpb->hFileInfo.ioVRefNum;
- err = DTGetIconSuite( orig_vRefNum, icon_selector,
- cpb->hFileInfo.ioFlFndrInfo.fdCreator,
- fileType,
- the_suite );
- if ( (*the_suite == NULL) && (second_selector != 0) )
- {
- err = DTGetIconSuite( orig_vRefNum, second_selector,
- cpb->hFileInfo.ioFlFndrInfo.fdCreator,
- fileType,
- the_suite );
- }
- if (err != noErr) // try any other local volumes
- {
- vpb.ioNamePtr = NULL;
- for (vpb.ioVolIndex = 1;
- PBGetVInfoSync( (ParmBlkPtr) &vpb ) == noErr;
- ++vpb.ioVolIndex )
- {
- if ( (vpb.ioVRefNum == orig_vRefNum)
- || !Is_local_volume( vpb.ioVRefNum ) )
- {
- continue;
- }
- err = DTGetIconSuite( vpb.ioVRefNum, icon_selector,
- cpb->hFileInfo.ioFlFndrInfo.fdCreator,
- fileType,
- the_suite );
- if ( (*the_suite == NULL) && (second_selector != 0) )
- {
- err = DTGetIconSuite( vpb.ioVRefNum,
- second_selector,
- cpb->hFileInfo.ioFlFndrInfo.fdCreator,
- fileType,
- the_suite );
- }
- if ( err == noErr )
- break;
- }
- }
-
- if (err != noErr) // resort to generic icon from System
- {
- UseResFile( 0 );
- err = Get1_resource_icons( the_suite, icon_id, icon_selector );
- if ( (*the_suite == NULL) && (second_selector != 0) )
- {
- err = Get1_resource_icons( the_suite, icon_id, second_selector );
- }
- if (*the_suite == NULL)
- {
- err = Get1_resource_icons( the_suite, genericDocumentIconResource,
- icon_selector );
- }
- }
- } // end else not in Finder
-
- UseResFile( save_resfile );
-
- return err;
- }
-
-